﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Net;

namespace WebLayer
{
    class CancelReservationPage: SiteWebPage
    {
        public CancelReservationPage(string eventId) :
            base("CancelReservation", GetActionPageUri("cancel_reservations"))
        {
            EventId = eventId;
        }

        string EventId;
        

        public override void Request()
        {
            base.Request();
        }

        protected override void OnRequest(System.Net.HttpWebRequest request)
        {
            base.OnRequest(request);

            Trace.WriteLine(PageName + ": Cancelling reservation for class #" + EventId);

            UTF8Encoding encoding = new UTF8Encoding();

            string postData = "EVENTID="+EventId;            
            byte[] data = encoding.GetBytes(postData);
            
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;

            Stream stream = request.GetRequestStream();
            stream.Write(data, 0, data.Length);
            stream.Close();            
        }

        protected override void OnResponse(System.Net.HttpWebRequest request, System.Net.HttpWebResponse response)
        {
            base.OnResponse(request, response);

            if (response.StatusCode != System.Net.HttpStatusCode.OK)
            {
                throw new WebException("Failed to make reservation");
            }
            
        }
    }
}
